package edu.mobbuzz.page.menu.contact;

import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import edu.mobbuzz.bean.Contact;
import edu.mobbuzz.facade.ContactFacade;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.inbox.RecepientContactPage;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;
import edu.mobbuzz.bean.Message;

public class ContactForm extends Menu{
    private Form contactForm;
    private Container contactPanel;
    private Label menuTitle; 
    private Label nameLbl;
    private Label idLbl;
    private Label sexLbl;
    private TextField nameTxt;
    private TextField idTxt;
    private ButtonGroup sexGroup;
    private RadioButton maleRadio;
    private RadioButton femaleRadio;
    private Command backCmd, saveCmd;
    private Contact contact;
    private int mode;
    private int index;
    private int recepientMode;
    private int messageType;
    private Message message;    
    private ContactFacade contactFacade;
    
    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public ContactFacade getContactFacade() {
        if(contactFacade==null)
            contactFacade = new ContactFacade();
        return contactFacade;
    }

    public int getRecepientMode() {
        return recepientMode;
    }

    public void setRecepientMode(int recepientMode) {
        this.recepientMode = recepientMode;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public int getMessageType() {
        return messageType;
    }


    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }
    
    public ContactForm(){
        contactForm = new Form();
        contactPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        nameLbl = new Label();
        nameTxt = new TextField();
        idLbl = new Label();
        idTxt = new TextField();
        sexLbl = new Label();
        sexGroup = new ButtonGroup();
        maleRadio = new RadioButton("Male"){
            public void actionPerformed(ActionEvent evt) {
                getSexSelection();               
            }   
        };
        femaleRadio = new RadioButton("Female"){
            public void actionPerformed(ActionEvent evt) {
                getSexSelection();               
            }   
        };
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToContactPage();
            }
        };
        
        saveCmd = new Command("Save") {
            public void actionPerformed(ActionEvent evt) {
                saveContact();
            }
        };
    }
    
    public void showPage(){
        
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);         
        
        nameLbl.setText("Name :");
        nameLbl.getStyle().setMargin(5, 0, 10, 2);
        nameLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        nameLbl.getStyle().setBgTransparency(0);  
        
        nameTxt.getStyle().setMargin(2, 0, 10, 10);
        
        idLbl.setText("Mobbuzz ID :");
        idLbl.getStyle().setMargin(5, 0, 10, 2);
        idLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        idLbl.getStyle().setBgTransparency(0);  
        
        idTxt.getStyle().setMargin(2, 0, 10, 10);
        
        sexLbl.setText("Sex :");
        sexLbl.getStyle().setMargin(5, 0, 10, 2);
        sexLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        sexLbl.getStyle().setBgTransparency(0);  
        
        sexGroup.add(maleRadio);
        sexGroup.add(femaleRadio);
        
        maleRadio.getStyle().setMargin(2, 0, 10, 0);
        maleRadio.getStyle().setFgColor(0x49BBF9);
        maleRadio.getStyle().setFgSelectionColor(0x49BBF9);
        maleRadio.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        maleRadio.getStyle().setBgTransparency(50);  
        
        femaleRadio.getStyle().setMargin(2, 0, 10, 0);
        femaleRadio.getStyle().setFgColor(0xF59FBC);
        femaleRadio.getStyle().setFgSelectionColor(0xF59FBC);
        femaleRadio.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        femaleRadio.getStyle().setBgTransparency(50);  
        
        if(getContact()!=null){
            nameTxt.setText(getContact().getName());
            idTxt.setText(getContact().getId());
            if(getContact().getSex()==0)
                maleRadio.setSelected(true);
            else if(getContact().getSex()==1)
                femaleRadio.setSelected(true);

        }
        
        contactPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));               
        contactPanel.addComponent(menuTitle);
        contactPanel.addComponent(nameLbl);
        contactPanel.addComponent(nameTxt);
        contactPanel.addComponent(idLbl);
        contactPanel.addComponent(idTxt);
        contactPanel.addComponent(sexLbl);
        contactPanel.addComponent(maleRadio);
        contactPanel.addComponent(femaleRadio);
           
        contactForm.setLayout(new BorderLayout());       
        contactForm.addComponent(BorderLayout.CENTER, contactPanel);  
        contactForm.addCommand(backCmd);
        contactForm.addCommand(saveCmd);
        contactForm.setCommandListener(getActionlistener());
        contactForm.show();
    }
    
    public void getSexSelection(){
        if(maleRadio.isSelected()){
            maleRadio.setSelected(true);
            femaleRadio.setSelected(false);
        }else if(femaleRadio.isSelected()){
            maleRadio.setSelected(false);
            femaleRadio.setSelected(true);
        }
    }
    
    public void backToContactPage(){
        ContactPage contactPage = new ContactPage();
        contactPage.setExitCmd(getExitCmd());
        contactPage.setActionlistener(getActionlistener());
        contactPage.showPage();
    }
   
    public void saveContact() {  
        Contact contactObj = null;
        
        if(getMode()==ModeString.NEW){
            contactObj = new Contact();
        }else if(getMode()==ModeString.EDIT){
            contactObj = getContact();
        }

        contactObj.setName(nameTxt.getText());
        contactObj.setId(idTxt.getText());

        if(maleRadio.isSelected()){
            contactObj.setSex(0);
        }else if(femaleRadio.isSelected()){
            contactObj.setSex(1);
        }
        
        if(getMode()==ModeString.NEW){
            getContactFacade().addContact(contactObj);
        }else if(getMode()==ModeString.EDIT){
            getContactFacade().updateContact(getIndex(), contact);
        }        

        
        if(getRecepientMode()!=0){
            RecepientContactPage recepientContactPage = new RecepientContactPage();
            recepientContactPage.setMessage(getMessage());
            recepientContactPage.setMode(getRecepientMode());
            recepientContactPage.setExitCmd(getExitCmd());
            recepientContactPage.setActionlistener(getActionlistener());
            recepientContactPage.showPage();
            recepientContactPage.setMessageType(getMessageType());
        }else{
            ContactPage contactPage = new ContactPage();
            contactPage.setExitCmd(getExitCmd());
            contactPage.setActionlistener(getActionlistener());
            contactPage.showPage();
        }
        
    }
       
    public String getMenuTitle() {
        return MenuString.NEW_CONTACT;
    }
        
}
 
